package com.pointi.emoTools;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class FileListData 
{
	public String _file_name = "";
	public String _file_path = "";
	public String _file_key_code = "";
	public String _call_date = "";
	public String _call_start_time = "";
	public String _call_end_time = "";
	public String _call_duration = "";
	public String _call_io_type = "";
	public String _call_key_code = "";
	public String _call_rec_type = "";
	public String _cust_id = "";
	public String _cust_name = "";
	public String _cust_no = "";
	public String _cust_tel = "";
	public String _usr_id = "";
	public String _usr_name = "";
	public String _usr_ext_no = "";

	public FileListData() { }

	public void InitializeData() 
	{
		_file_name = "";
		_file_path = "";
		_file_key_code = "";
		_call_date = "";
		_call_start_time = "";
		_call_end_time = "";
		_call_duration = "";
		_call_io_type = "";
		_call_key_code = "";
		_call_rec_type = "";
		_cust_id = "";
		_cust_name = "";
		_cust_no = "";
		_cust_tel = "";
		_usr_id = "";
		_usr_name = "";
		_usr_ext_no = "";	
	}

	
	//// 20140702 // for ONLY WAV // Auto Insert XML DATA // SOC //------------------------------------------------------------//
	private static int g_cnt = 1 ;
	private String fnGetDuration(int fileLength)
	{
		int SampleRate = 8000 ;
		int Channels = 1 ;
		int BitsPerSamples = 16 ;
		return String.valueOf(fileLength / (SampleRate * Channels * BitsPerSamples / 8)) ;
	}
	private String fnCodeGenerator(int limit)
	{
		String id = String.valueOf(FileListData.g_cnt) ;
		int id_l = id.length() ;
		int zero_l = 0 ;
		if (id_l < limit)
		{
			zero_l = (limit - id_l) ;
			for (int i=0; i<zero_l; i++) { id = ("0" + id) ; }
		}
		return id ;
	}
	public FileListData fnSetTempData(byte [] inFile, String fileName, int fileLength, emoJDBC jdbc) throws Exception
	{
		FileListData _FileList = new FileListData();
		_FileList.InitializeData();
		// Element 
		_FileList._file_name = fileName.replace("xml", "wav") ;
		_FileList._file_path = "C:\\" ;
		_FileList._file_key_code = "7" + this.fnCodeGenerator(5) ;
		_FileList._call_start_time = jdbc.fnGetStartTime() ;
		_FileList._call_duration = this.fnGetDuration(fileLength) ;
		_FileList._call_end_time = jdbc.fnGetEndTime(_FileList._call_duration) ;
		_FileList._call_io_type = "0" ;
		_FileList._call_key_code = "2" + this.fnCodeGenerator(5) ;
		_FileList._call_rec_type = "99" ;
		_FileList._cust_id = "1" + this.fnCodeGenerator(5) ;
		_FileList._cust_name = "한지민" ;
		_FileList._cust_no = "821105" + this.fnCodeGenerator(7) ;
		_FileList._cust_tel = "010" + this.fnCodeGenerator(8) ;
		_FileList._usr_id = "" + this.fnCodeGenerator(5) ;
		_FileList._usr_name = "AN" + this.fnCodeGenerator(5) ;
		_FileList._usr_ext_no = "3" + this.fnCodeGenerator(5) ;
		return _FileList;		
	}
	////20140702 // for ONLY WAV // Auto Insert XML DATA // EOC //------------------------------------------------------------//

	public FileListData ReadFileList(byte [] inFile) throws Exception 
	{
		FileListData _FileList = new FileListData();
		_FileList.InitializeData();
		try 
		{
			DocumentBuilderFactory docBuildFact = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuild = docBuildFact.newDocumentBuilder();
			Document doc = docBuild.parse(new ByteArrayInputStream(inFile));
			doc.getDocumentElement().normalize();

			String s_node = doc.getDocumentElement().getNodeName();

			if (s_node.equals("Data")) 
			{
				// FileList
				NodeList filelist = doc.getElementsByTagName("Data");

				for (int i = 0; i < filelist.getLength(); i++) 
				{	 
					Node fileNode = filelist.item(i);

					if (fileNode.getNodeType() == Node.ELEMENT_NODE) 
					{
						// Element 
						Element fileElmnt = (Element) fileNode;

						// file Name
						NodeList fileNameList= fileElmnt.getElementsByTagName("file_name");
						Element fileNameElmnt = (Element) fileNameList.item(0);
						Node fileName = fileNameElmnt.getFirstChild();
						_FileList._file_name = fileName.getNodeValue();

						// file Path
						NodeList filePathList= fileElmnt.getElementsByTagName("file_path");
						Element filePathElmnt = (Element) filePathList.item(0);
						Node filePath = filePathElmnt.getFirstChild();
						_FileList._file_path = filePath.getNodeValue();

						// file Key Code
						NodeList fileKeyCodeList= fileElmnt.getElementsByTagName("file_key_code");
						Element fileKeyCodeElmnt = (Element) fileKeyCodeList.item(0);
						Node fileKeyCode = fileKeyCodeElmnt.getFirstChild();
						_FileList._file_key_code = fileKeyCode.getNodeValue();

						// call Start Time
						NodeList callStartTimeList= fileElmnt.getElementsByTagName("call_start_time");
						Element callStartTimeElmnt = (Element) callStartTimeList.item(0);
						Node callStartTime = callStartTimeElmnt.getFirstChild();
						_FileList._call_start_time = callStartTime.getNodeValue();

						// call End Time
						NodeList callEndTimeList= fileElmnt.getElementsByTagName("call_end_time");
						Element callEndTimeElmnt = (Element) callEndTimeList.item(0);
						Node callEndTime = callEndTimeElmnt.getFirstChild();
						_FileList._call_end_time = callEndTime.getNodeValue();

						// call Duration
						NodeList callDurationList= fileElmnt.getElementsByTagName("call_duration");
						Element callDurationElmnt = (Element) callDurationList.item(0);
						Node callDuration = callDurationElmnt.getFirstChild();
						_FileList._call_duration = callDuration.getNodeValue();

						// call I/O Type
						NodeList callIOTypeList= fileElmnt.getElementsByTagName("call_io_type");
						Element callIOTypeElmnt = (Element) callIOTypeList.item(0);
						Node callIOType = callIOTypeElmnt.getFirstChild();
						_FileList._call_io_type = callIOType.getNodeValue();

						// call Key Code
						NodeList callKeyCodeList= fileElmnt.getElementsByTagName("call_key_code");
						Element callKeyCodeElmnt = (Element) callKeyCodeList.item(0);
						Node callKeyCode = callKeyCodeElmnt.getFirstChild();
						_FileList._call_key_code = callKeyCode.getNodeValue();

						// call Rec Type
						NodeList callRecTypeList= fileElmnt.getElementsByTagName("call_rec_type");
						Element callRecTypeElmnt = (Element) callRecTypeList.item(0);
						Node callRecType = callRecTypeElmnt.getFirstChild();
						_FileList._call_rec_type = callRecType.getNodeValue();

						// customer ID
						NodeList custIDList= fileElmnt.getElementsByTagName("cust_id");
						Element custIDElmnt = (Element) custIDList.item(0);
						Node custID = custIDElmnt.getFirstChild();
						_FileList._cust_id = custID.getNodeValue();

						// customer Name
						NodeList custNameList= fileElmnt.getElementsByTagName("cust_name");
						Element custNameElmnt = (Element) custNameList.item(0);
						Node custName = custNameElmnt.getFirstChild();
						_FileList._cust_name = custName.getNodeValue();

						// customer Number
						NodeList custNumberList= fileElmnt.getElementsByTagName("cust_no");
						Element custNumberElmnt = (Element) custNumberList.item(0);
						Node custNumber = custNumberElmnt.getFirstChild();
						_FileList._cust_no = custNumber.getNodeValue();

						// customer Telephone
						NodeList custTelList= fileElmnt.getElementsByTagName("cust_tel");
						Element custTelElmnt = (Element) custTelList.item(0);
						Node custTel = custTelElmnt.getFirstChild();
						_FileList._cust_tel = custTel.getNodeValue();

						// user ID
						NodeList usrIDList= fileElmnt.getElementsByTagName("usr_id");
						Element usrIDElmnt = (Element) usrIDList.item(0);
						Node usrID = usrIDElmnt.getFirstChild();
						_FileList._usr_id = usrID.getNodeValue();

						// user Name
						NodeList usrNameList= fileElmnt.getElementsByTagName("usr_name");
						Element usrNameElmnt = (Element) usrNameList.item(0);
						Node usrName = usrNameElmnt.getFirstChild();
						_FileList._usr_name = usrName.getNodeValue();

						// user ext no
						NodeList usrExtNoList= fileElmnt.getElementsByTagName("usr_ext_no");
						Element usrExtNoElmnt = (Element) usrExtNoList.item(0);
						Node usrExtNo = usrExtNoElmnt.getFirstChild();
						_FileList._usr_ext_no = usrExtNo.getNodeValue();						
					}
				}
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}				
		return _FileList;
	}

	public void WriteLogConfigurationData(FileListData inFileList)
	{
		System.out.println("----------------- database ------------------");
		System.out.println("File Name		: " + inFileList._file_name);
		System.out.println("File Path		: " + inFileList._file_path);
		System.out.println("File KeyCode	: " + inFileList._file_key_code);
		System.out.println("Call Date		: " + "");
		System.out.println("Call Start Time	: " + inFileList._call_start_time);
		System.out.println("Call End Time	: " + inFileList._call_end_time);
		System.out.println("Call Duration	: " + inFileList._call_duration);
		System.out.println("Call IO Type	: " + inFileList._call_io_type);
		System.out.println("Call Key Code	: " + inFileList._call_key_code);
		System.out.println("Call Rec Type	: " + inFileList._call_rec_type);
		System.out.println("Customer ID		: " + inFileList._cust_id);
		System.out.println("Customer Name	: " + inFileList._cust_name);
		System.out.println("Customer No		: " + inFileList._cust_no);
		System.out.println("Customer Tel	: " + inFileList._cust_tel);
		System.out.println("Agent ID		: " + inFileList._usr_id);
		System.out.println("Agent Name		: " + inFileList._usr_name);
		System.out.println("Agent Ext.No	: " + inFileList._usr_ext_no);
		System.out.println("---------------------------------------------");
	}
}
